/**
 * ����Tag
 * @author lyoe.cc
 * @author goodtiger
 */
import org.grails.taggable.*

public class Topic implements Taggable {
	
    //Fields
    String name
    String imgUrl
    byte[] image
    String description
    int limitSpeakerNum = 4
    int currentSpeakerNum = 1
    TopicCategory topicCategory
    String status = 'INIT'
    String bestStatus = 'NORMAL'
    User owner
    Date endTime = new Date().plus(1)
    Date dateCreated
    Date lastUpdated
	
    static constraints = {
        name(nullable:false, size:1..100)
        topicCategory(nullable:false)
        endTime(nullable:false)
        image(nullable:true, maxSize:1024*20)
        imgUrl(nullable:true)
        description(nullable:false, size:1..1000)
        status(inList:['INIT','START','END'])
        bestStatus(inList:['NORMAL','BEST','WORST'])
    }
	
    static belongsTo = [User]
    static hasMany =[opinions:Opinion, topicSpeakerShips: TopicSpeakerShip]
    //	static trasients = ["image"]
	
    static mapping = {
        sort dateCreated:"desc"
    }

    static mappedby = [opinions:"topic"]

    String toString(){
        "$name"
    }

    def speakers() {
        return topicSpeakerShips.collect{it.user}
    }

    List addSpeaker(User speaker) {
        if(TopicSpeakerShip.link(speaker, this)) 
        return speakers()
    }

    List removeSpeaker(User speaker) {
        TopicSpeakerShip.unlink(speaker, this)

        return speakers()
    }
}
